package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.doris.KanbanFlowSummaryCube;
import com.hllxd.hism.star.basic.service.service.ReportService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * @author xiyong.lxy
 * @date 2022-03-13 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/report")
public class ReportController2 {

    @Autowired
    private ReportService reportService;

    @Data
    public static class Params {
        public Integer p1;
        public String p2;
        public String p3;

        public String dt0;
        public String dt1;

        public String dt;
        public String dt2;


        public String items;

        public String field;
        public String adEtpname;
        public String adUserid;
        public String adDisptype;
        public String adId;
        public String imei;

        public String bizTime;
        public String topicId;
        public String biz;
        public String platform;
        public String infoEtpid;
        public String infoEtpname;
        public String infoBuid;
        public String infoBuname;
        public String infoId;
        public String infoLocal1;
        public String infoCate2;
        public String infoLocal1Name;
        public String infoCate2Name;
        public String dispLocal1Name;
        public String dispCate2Name;
        public String startDt;
        public String endDt;

    }

    @RestGrpcMapping("/listFengkong.do")
    public NTSTATUS listFengkong(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            result.setData(reportService.listFengkong(aa.dt0, aa.dt1, aa.biz));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getCustomerChart.do")
    public NTSTATUS getCustomerChart(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            HashMap<String, String> dimMap = new HashMap<>();
            dimMap.put("ad_etpname", aa.getAdEtpname());
            dimMap.put("ad_userid", aa.getAdUserid());
            dimMap.put("ad_disptype", aa.getAdDisptype());

            String metricCol = getMetricCol(aa);
            result.setData(reportService.getCustomerChart(metricCol, dimMap, "ddb58_zp_bigdata_db.app_zp_gj_fk_summary_flow_buser_4doris"));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listCustomerReport.do")
    public NTSTATUS listCustomerReport(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            result.setData(reportService.listCustomerReport(aa.getBizTime(), aa.getBiz(), aa.getAdEtpname()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listSummaryReport.do")
    public NTSTATUS listSummaryReport(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            KanbanFlowSummaryCube aa = params.driverObject(KanbanFlowSummaryCube.class);
            result.setData(reportService.listSummaryReport(aa));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

//    @RestGrpcMapping("/listSummaryFlow.do")
//    public NTSTATUS listSummaryFlow(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("params: {}", params);
//        try {
//            KanbanFlowSummaryFlow aa = params.driverObject(KanbanFlowSummaryFlow.class);
//            result.setData(reportService.selectSummaryFlow(aa));
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

    @RestGrpcMapping("/listUserReport.do")
    public NTSTATUS listUserReport(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            result.setData(reportService.listUserReport(aa.getBizTime(), aa.getBiz(), aa.getImei()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getUserChart.do")
    public NTSTATUS getUserChart(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            HashMap<String, String> dimMap = new HashMap<>();
            dimMap.put("imei", aa.getImei());
            dimMap.put("ad_disptype", aa.getAdDisptype());

            String metricCol = getMetricCol(aa);
            result.setData(reportService.getCustomerChart(metricCol, dimMap, "ddb58_zp_bigdata_db.app_zp_gj_fk_summary_flow_user_4doris"));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listAdReport.do")
    public NTSTATUS listAdReport(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            result.setData(reportService.listAdReport(aa.getBizTime(), aa.getBiz(), aa.getAdId()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getAdChart.do")
    public NTSTATUS getAdChart(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            HashMap<String, String> dimMap = new HashMap<>();
            dimMap.put("ad_id", aa.getAdId());
            dimMap.put("ad_disptype", aa.getAdDisptype());

            String metricCol = getMetricCol(aa);
            result.setData(reportService.getCustomerChart(metricCol, dimMap, "ddb58_zp_bigdata_db.app_zp_gj_fk_summary_flow_ad_4doris"));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/listEtpReport.do")
    public NTSTATUS listEtpReport(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            result.setData(reportService.listEtpReport(aa.getBizTime(), aa.getBiz(), aa.getAdEtpname()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getEtpChart.do")
    public NTSTATUS getEtpChart(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            HashMap<String, String> dimMap = new HashMap<>();
            dimMap.put("ad_etpname", aa.getAdEtpname());
            dimMap.put("ad_disptype", aa.getAdDisptype());

            String metricCol = getMetricCol(aa);
            result.setData(reportService.getCustomerChart(metricCol, dimMap, "ddb58_zp_bigdata_db.app_zp_gj_fk_summary_flow_etp_4doris"));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 客户效果分析
     * @param params 参数
     * @param result 结果
     * @return 返回
     */
    @RestGrpcMapping("/getSummaryReportChart.do")
    public NTSTATUS getSummaryReportChart(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            KanbanFlowSummaryCube aa = params.driverObject(KanbanFlowSummaryCube.class);

            HashMap<String, String> dimMap = new HashMap<>();
            dimMap.put("topic_id", aa.getTopicId());
            dimMap.put("biz", aa.getBiz());
            dimMap.put("platform", aa.getPlatform());
            dimMap.put("info_etpid", aa.getInfoEtpid());
            dimMap.put("info_etpname", aa.getInfoEtpname());
            dimMap.put("info_buid", aa.getInfoBuid());
            dimMap.put("info_id", aa.getInfoId());
            dimMap.put("info_type", aa.getInfoType());

            dimMap.put("info_local1", aa.getInfoLocal1());
            dimMap.put("info_cate2", aa.getInfoCate2());
            dimMap.put("disp_local1", aa.getDispLocal1());
            dimMap.put("disp_cate2", aa.getDispCate2());

            dimMap.put("info_local1_name", aa.getInfoLocal1Name());
            dimMap.put("info_cate2_name", aa.getInfoCate2Name());
            dimMap.put("disp_local1_name", aa.getDispLocal1Name());
            dimMap.put("disp_cate2_name", aa.getDispCate2Name());
            String metricCol = getMetricCol(aa.getField());

            result.setData(reportService.getCustomerChart(metricCol, dimMap, "ddb58_zp_bigdata_db.ads_zp_n_kanban_flow_summary_cube"));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 客户效果分析
     * @param params 参数
     * @param result 结果
     * @return 返回
     */
    @RestGrpcMapping("/getSummaryReportEntChart.do")
    public NTSTATUS getSummaryReportEntChart(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            KanbanFlowSummaryCube aa = params.driverObject(KanbanFlowSummaryCube.class);

            HashMap<String, String> filterMap = new HashMap<>();
            filterMap.put("dimension", aa.dimension);
            filterMap.put("biz", aa.biz);
            filterMap.put("platform", aa.platform);
            filterMap.put("info_etpname", aa.infoEtpname);

            String columnName = getMetricCol(aa.getField());
            String tableName= "ddb58_zp_bigdata_db.ads_zp_n_kanban_flow_summary_cube";

            result.setData(reportService.getSummaryReportEntChart(columnName, tableName, filterMap));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    @RestGrpcMapping("/getSummaryReportEntChart2.do")
    public NTSTATUS getSummaryReportEntChart2(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            KanbanFlowSummaryCube aa = params.driverObject(KanbanFlowSummaryCube.class);

            HashMap<String, String> filterMap = new HashMap<>();
            filterMap.put("dimension", aa.dimension);
            filterMap.put("ent_name", aa.infoEtpname);

            String columnName = getMetricCol(aa.getField());
            String tableName= "ddb58_zp_bigdata_db.ads_zp_n_kanban_gaap";

            result.setData(reportService.getSummaryReportEntChart(columnName, tableName, filterMap));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

//    @RestGrpcMapping("/getSummaryFlowChart.do")
//    public NTSTATUS getSummaryFlowChart(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("params: {}", params);
//        try {
//            KanbanFlowSummaryFlow aa = params.driverObject(KanbanFlowSummaryFlow.class);
//
//            HashMap<String, String> dimMap = new HashMap<>();
//            dimMap.put("topic_id", aa.getTopicId());
//            dimMap.put("biz", aa.getBiz());
//            dimMap.put("platform", aa.getPlatform());
//            dimMap.put("slot", aa.getSlot());
//            dimMap.put("requesttype", aa.getRequesttype());
//            dimMap.put("disp_local1", aa.getDispLocal1());
//            dimMap.put("disp_cate3", aa.getDispCate3());
//            String metricCol = getMetricCol(aa.getField());
//            result.setData(reportService.getCustomerChart(metricCol, dimMap, "ddb58_zp_bigdata_db.ads_zp_n_kanban_flow_summary_flow"));
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

    @NotNull
    private String getMetricCol(Params aa) {
        StringBuilder sb = new StringBuilder();
        for (char c : aa.getField().toCharArray()) {
            if (Character.isUpperCase(c)) {
                sb.append("_").append(c);
            } else {
                sb.append(c);
            }
        }
        return sb.toString().toLowerCase();
    }

    private String getMetricCol(String field) {
        StringBuilder sb = new StringBuilder();
        for (char c : field.toCharArray()) {
            if (Character.isUpperCase(c)) {
                sb.append("_").append(c);
            } else {
                sb.append(c);
            }
        }
        return sb.toString().toLowerCase();
    }
}
